﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Com.FastSearch.SharePoint.Interfaces;

namespace Com.FastSearch.SharePoint.WebControls.ESPSearchWebParts.UI
{
    /// <summary>
    /// A web-part that renders all navigators in the search result. Uses an instance of
    /// <see cref="ESPNavigator"/> to render each navigator.
    /// </summary>
    public class ESPNavigators : ESPSearchWebPart
    {
        #region Private variables

        private const string _searchConfigurationCategory = "Search configuration";
        private int _numberOfModifiers = 5;
        private bool _showMoreLink = true;
        private IESPSearchResult _searchResult;

        #endregion

        #region WebPart Properties

        /// <summary>
        /// The number of items to show for the navigators. Additional items will be available
        /// via links if <see cref="ShowMoreLink"/> is true.
        /// </summary>
        [Category(_searchConfigurationCategory), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Number of items to display"), WebDescription("Set the number of items to display here.")]
        public int NumberOfModifiers
        {
            get { return _numberOfModifiers; }
            set { _numberOfModifiers = value; }
        }

        /// <summary>
        /// If enabled, items exceeding the number defined in <see cref="NumberOfModifiers"/>
        /// will be available via "Show more items..." links.
        /// </summary>
        [Category(_searchConfigurationCategory), Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Show \"more items\" link"), WebDescription("Make additional navigator items available via \"show more items\" link")]
        public bool ShowMoreLink
        {
            get { return _showMoreLink; }
            set { _showMoreLink = value; }
        }

        #endregion

        #region ESPSearchWebPart members

        /// <summary>
        /// Set any parameters on the search required by this web-part.
        /// </summary>
        protected override void ConfigureSearch()
        {
        }

        /// <summary>
        /// Set the search-result. Called when the search has been performed.
        /// </summary>
        /// <param name="SearchResult">The search result</param>
        public override void SetSearchResult(IESPSearchResult SearchResult)
        {
            _searchResult = SearchResult;
        }

        /// <summary>
        /// Get the XSL stylesheet bundled with the web-part.
        /// </summary>
        /// <returns>The default XSL to be used when rendering this web-part</returns>
        public override string GetEmbeddedXsl()
        {
            return LoadXslFromAssembly("Navigator.xslt");
        }

        /// <summary>
        /// Create the child controls of this web-part. Adds an <see cref="ESPNavigator"/>
        /// control for each of the navigators in the search result.
        /// </summary>
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            if (_searchResult == null || _searchResult.Navigators == null)
            {
                return;
            }

            try
            {
                int subId = 0;
                foreach (IESPNavigator navigator in _searchResult.Navigators)
                {
                    ESPNavigator navigatorWebPart = new ESPNavigator();
                    navigatorWebPart.NavigatorName = navigator.Name;
                    navigatorWebPart.SetSearchResult(_searchResult);
                    navigatorWebPart.Title = navigator.DisplayName;
                    navigatorWebPart.ID = ID + (++subId);
                    navigatorWebPart.Xsl = Xsl;
                    Controls.Add(navigatorWebPart);
                }
            }
            catch (Exception e)
            {
                Controls.Add(new LiteralControl(e.Message + ". " + e.StackTrace));
            }
        }

        #endregion
    }
}
